﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.Globalization;

namespace MvcCommon
{
    public class MyRequiredAttributeAdapter : System.Web.Mvc.RequiredAttributeAdapter
    {
        public MyRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, RequiredAttribute attribute) :
            base(metadata, context, attribute) { }
        public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
        {
            string errorMessage = string.Format("'{0}'不能为空。", Metadata.DisplayName);
            return new[] { new ModelClientValidationRequiredRule(errorMessage) };
        }
    }

    //public class ClientNumberValidatorProvider : ClientDataTypeModelValidatorProvider
    //{
    //    public override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata,
    //                                                           ControllerContext context)
    //    {
    //        bool isNumericField = base.GetValidators(metadata, context).Any();
    //        if (isNumericField)
    //            yield return new ClientSideNumberValidator(metadata, context);
    //    }
    //}

    //public class ClientSideNumberValidator : ModelValidator
    //{
    //    public ClientSideNumberValidator(ModelMetadata metadata,
    //        ControllerContext controllerContext)
    //        : base(metadata, controllerContext) { }

    //    public override IEnumerable<ModelValidationResult> Validate(object container)
    //    {
    //        yield break; // Do nothing for server-side validation 
    //    }

    //    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules()
    //    {
    //        yield return new ModelClientValidationRule
    //        {
    //            ValidationType = "number",
    //            ErrorMessage = string.Format(CultureInfo.CurrentCulture,
    //                                         ValidationMessages.MustBeNumber,
    //                                         Metadata.GetDisplayName())
    //        };
    //    }
    //}

}